#include "peopletreemodel.h"
#include <QSqlQuery>
#include <QDebug>

PeopleTreeModel::PeopleTreeModel(QObject *parent) :
    QAbstractItemModel(parent), m_rootNode(0)
{
    load();
}

PeopleTreeModel::~PeopleTreeModel()
{
    delete m_rootNode;
}

QModelIndex PeopleTreeModel::index(int row, int column, const QModelIndex &parent) const
{
    if(!m_rootNode)
        return QModelIndex();
    PeopleTreeNode *parentNode = nodeFromIndex(parent);
    return createIndex(row, column, parentNode->child(row));
}

QModelIndex PeopleTreeModel::parent(const QModelIndex &child) const
{
    PeopleTreeNode *node = nodeFromIndex(child);
    if(!node)
        return QModelIndex();
    PeopleTreeNode *parentNode = node->parent();
    if(!parentNode)
        return QModelIndex();
    PeopleTreeNode *grandparentNode = parentNode->parent();
    if(!grandparentNode)
        return QModelIndex();

    int row = grandparentNode->childIndex(parentNode);
    return createIndex(row, child.column(), parentNode);
}

int PeopleTreeModel::rowCount(const QModelIndex &parent) const
{
    PeopleTreeNode *parentNode = nodeFromIndex(parent);
    if(!parentNode)
        return 0;
    return parentNode->childCount();
}

int PeopleTreeModel::columnCount(const QModelIndex &) const
{
    return 1;
}

QVariant PeopleTreeModel::data(const QModelIndex &index, int role) const
{
    PeopleTreeNode *node = nodeFromIndex(index);
    if(!node)
        return QVariant();

    if(role == Qt::DisplayRole)
        return node->name();
    if(role == Qt::DecorationRole)
        return node->icon();

    return QVariant();
}

QVariant PeopleTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        if(section == 0)
            return tr("People");

    }
    return QVariant();
}

void PeopleTreeModel::setRootNode(PeopleTreeNode *newRootNode)
{
    delete m_rootNode;
    m_rootNode = newRootNode;
    reset();
}

void PeopleTreeModel::load()
{
    QSqlQuery sql("SELECT `id`,`name`,`parent` FROM `groups` WHERE 1;");

    QHash<int, int> id_parent;
    while(sql.next()) {
        int id = sql.value(0).toInt();
        QString name = sql.value(1).toString();
        int parent = sql.value(2).toInt();

        PeopleTreeNode *gn = new PeopleTreeNode("group", name, id);
        m_groups.insert(id, gn);
        id_parent.insert(id, parent);
    }

    PeopleTreeNode *root_n = new PeopleTreeNode("", "", 0);
    m_groups.insert(0, root_n);
    QHashIterator<int, PeopleTreeNode *> it(m_groups);
    it.next();
    while (it.hasNext()) {
        it.next();
        int id = it.key();
        PeopleTreeNode *cn = it.value();

        int p_id = id_parent.value(id);
        if (m_groups.contains(p_id)) {
            m_groups[p_id]->appendChild(cn);
        } else {
            m_groups[0]->appendChild(cn);
            qWarning() << "Group with id" << p_id << "didn't exist";
        }
    }

    sql.exec("SELECT `id`,CONCAT(`people`.`name_first`, ' ', `people`.`name_last`, ' ', `people`.`name_middle`),"
             "`type`,`group` FROM `people` WHERE 1;");
    while (sql.next()) {
        int id = sql.value(0).toInt();
        QString name = sql.value(1).toString();
        QString type = sql.value(2).toString();
        int group = sql.value(3).toInt();

        PeopleTreeNode *pn = new PeopleTreeNode(type, name, id);
        m_groups[group]->appendChild(pn);
    }

    setRootNode(root_n);
}


PeopleTreeNode *PeopleTreeModel::nodeFromIndex(const QModelIndex &index) const
{
    if(index.isValid())
        return static_cast<PeopleTreeNode *>(index.internalPointer());
    else
        return m_rootNode;
}
